package com.example.petmall.seckill.controller;

import com.example.petmall.seckill.service.SeckillService;
import com.example.petmall.seckill.to.SecKillRedisTo;
import com.qianji.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author liang zai
 * @date 2022/5/25 16:00
 * @description
 */
@Controller
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    @GetMapping("/getCurrentSeckillSkus")
    @ResponseBody
    public R getCurrentSeckillSkus(){
        List<SecKillRedisTo> list = seckillService.getCurrentSeckillSkus();
        System.out.println("sdfsfsfsfsfsdfs"+list);
        return R.ok().setData(list);
    }

    @GetMapping("sku/skuInfo/{skuId}")
    @ResponseBody
    public R getSeckillSkuInfo(@PathVariable("skuId") Long skuId) throws InterruptedException {
//        Thread.sleep(300);
        SecKillRedisTo redisTo = seckillService.getSeckillSkuInfo(skuId);
        return R.ok().setData(redisTo);
    }

    @GetMapping("/addSeckillCart")
    public String addSeckillCart(@RequestParam("seckillId") String seckillId,
                            @RequestParam("key") String key,
                            @RequestParam("num") Integer num, HttpServletRequest model){
        //需要判断当前用户是否登录，登录存放在Session中，整合session redis
        String orderSn = seckillService.addSeckillCart(seckillId, key, num);
        model.setAttribute("orderSn",orderSn);
        return "success";
    }
}
